<?php
/**
 * Created by PhpStorm.
 * User: fengzb
 * Date: 2017/11/26
 * Time: 17:22
 */

namespace app\index\controller;

use app\index\Base;
use think\Db;
use think\Session;
use think\Url;

class User extends Base
{

    private $param;
    private $user;
    public function __construct(){
        parent::__construct();

        //提交的参数
        $this->param = $this->request->param();
        $this->user = Session::get('user');

    }
    public function index()
    {

      $maps['uid'] = $this->user['m_uid'];
      $maps['status'] = 0;
      $maps['pay_status'] = 0;
      $arr['daifu'] = Db::table('kyf_order')->where($maps)->count(); 
      $maps['pay_status'] = 1;
      $arr['daifa'] = Db::table('kyf_order')->where($maps)->count();
      $maps['status'] = 1;
      $arr['daishou'] = Db::table('kyf_order')->where($maps)->count();
      $maps['status'] = 2;
      $arr['daiping'] = Db::table('kyf_order')->where($maps)->count();
      $this->view->assign('arr', $arr);
      $arrs = Db::table('kyf_member_base')->where('m_uid='.$this->user['m_uid'])->find();
        $this->view->assign('arrs', $arrs);
      return $this->view->fetch();
    }
    public function order()
    {

      $type = $this->param['type'];
      $maps['uid'] = $this->user['m_uid'];
      $arrs['zong'] = Db::table('kyf_order')->where($maps)->count();
      $mapsd['status'] = 0;
        $mapsd['pay_status'] = 0;
        $arrs['daifu'] = Db::table('kyf_order')->where($mapsd)->count();
        $mapsd['pay_status'] = 1;
        $mapsd['status'] = 1;
        $arrs['daishou'] = Db::table('kyf_order')->where($mapsd)->count(); 
        $mapsd['status'] = 2;
        $arrs['daiping'] = Db::table('kyf_order')->where($mapsd)->count();
        $mapsds['status'] = 4;
        $arrs['quxiao'] = Db::table('kyf_order')->where($mapsds)->count();
      if($type==1){
        $maps['status'] = 0;
        $maps['pay_status'] = 0;
      }elseif ($type==2) {
        $maps['status'] = 0;
        $maps['pay_status'] = 1; 
      }elseif ($type==3) {
        $maps['status'] = 1;
        $maps['pay_status'] = 1;
      }elseif ($type==4) {
        $maps['status'] = 2;
        $maps['pay_status'] = 1;
      }elseif ($type==5) {
        $maps['status'] = 4;

      }    
          
      $zong = Db::table('kyf_order')->where($maps)->count();
      $arr = Db::table('kyf_order')->where($maps)->select();
      foreach ($arr as $key => $value) {
        $res = $this->shangxiang($value['cart_idstr']);
        $arr[$key]['goods'] = $res['goods'];
        $arr[$key]['zong'] = $res['zong'];
        
          $arr[$key]['real_money'] = fmoney($value['real_money'],false);
          $arr[$key]['postfee'] = fmoney($value['postfee'],false);
      }
    
      if (Session::get('jiatu')!='') {
       $add['imgs'] = Session::get('jiatu');
       Db::table('kyf_comment')->where("comment_id=".Session::get('jiaid'))->update($add);
       Session::delete('jiatu');  
       Session::delete('jiaid');  
      }
      //$add['imgs'] = Session::get('jiatu');
            //Session::delete('jiatu');
      //print_r($arr);exit;
      $this->view->assign('type', $type);
      $this->view->assign('arrs', $arrs);
      $this->view->assign('arr', $arr);
      $this->view->assign('zong', $zong);
      return $this->view->fetch();
    }

    public function shangxiang($str){
         $maps['cart_id'] = array('in',$str);   
         $maps['uid'] = array('eq',$this->user['m_uid']);
         $list = Db::table('kyf_shopping_cart')->where($maps)->select();
         $goodstotal = 0;
        foreach ($list as $key => $val){
            $goods = Db::table('kyf_goods')->where('goods_id=' . $val['goods_id'])->find();
            $list[$key]['goods_name'] = $goods['goods_name'];
            $picarr = explode(',',$goods['goods_pics']);
            $list[$key]['goods_pics'] = $picarr[0];
            $list[$key]['buy_price']  = fmoney($val['buy_price'],false);
            $goodstotal+= $val['buy_num'];
        }
        $res['goods'] = $list;
        $res['zong'] = $goodstotal;
        return $res;


    }
    public function shoucang()
    {
      $maps['uid'] = $this->user['m_uid'];    
      $arr = Db::table('kyf_shoucang')->where($maps)->select(); 
      foreach ($arr as $key => $value) {
          $re = Db::table('kyf_goods')->where("goods_id=".$value['gid'])->find();
          $arr[$key]['goods_name'] = $re['goods_name'];
          $picarr = explode(',',$re['goods_pics']);
          $arr[$key]['goods_pic'] = $picarr[0];
          $arr[$key]['buytype'] = $re['buytype'];
          $arr[$key]['price'] = fmoney($re['price'],false);

      }
      $this->view->assign('arr', $arr);
      return $this->view->fetch();
    }
    public function pingjia(){
        if($this->request->isPost()){
            
            $add['order_id'] = $_POST['orderid'];
            $add['contents'] = $_POST['miaoshu'];
            $add['store_start'] = $_POST['score'];
            $add['ctype'] = 2;
            $add['create_time'] = time();
            $add['uid'] = $this->user['m_uid'];
            $add['goods_id'] = $_POST['goods_idstr'];

            //$add['imgs'] = Session::get('jiatu');
            //Session::delete('jiatu');
            $re = Db::table('kyf_comment')->insert($add,false,true);
            Session::set('jiaid',$re);
            echo $re;exit;
        }else{
          $order_id = $this->param['oid'];
          $arr = Db::table('kyf_order')->where("order_id=".$order_id)->find();
          $res = $this->shangxiang($arr['cart_idstr']);
          $arr['goods'] = $res['goods'];
          $this->view->assign('arr', $arr);
          $this->view->assign('order_id', $order_id);
          return $this->view->fetch();   
        }
       
    }
    public function fankui(){
       if($this->request->isPost()){
            
            $add['bk_name'] = $_POST['bk_name'];
            $add['bk_remark'] = $_POST['bk_remark'];
            $add['bk_mobile'] = $_POST['bk_mobile'];
            $add['create_time'] = time();
            $add['bk_uid'] = $this->user['m_uid'];
   
            $re = Db::table('kyf_feedback')->insert($add,false,true);
            echo $re;exit;
        }else{
          
          return $this->view->fetch();   
        }
    }
    public function gainan(){
       if($this->request->isPost()){
            
            $xing = $_POST['xing'];
            $ty = $_POST['xing'];
            if ($ty==2) {
                if ($xing=='男') {
                $add['m_sex'] = 1;
                }else{
                    $add['m_sex'] = 2;
                }
            }else{
               $add['m_riqi'] = $xing; 
            }
            
            Db::table('kyf_member_base')->where('m_uid='.$this->user['m_uid'])->update($add);
        }
    }
    public function nicheng(){
        if($this->request->isPost()){
            $add['m_nickname'] = $_POST['name'];
Db::table('kyf_member_base')->where('m_uid='.$this->user['m_uid'])->update($add);
echo 1;exit;
        }else{


            $nicheng = $this->param['nicheng'];
            $this->view->assign('nicheng', $nicheng);
        return $this->view->fetch(); 
        }
    }
    public function xinxi(){
        $arr = Db::table('kyf_member_base')->where('m_uid='.$this->user['m_uid'])->find();
        $this->view->assign('arr', $arr);
        return $this->view->fetch(); 
    }
     public function dizhi(){
 
        $arr = Db::table('kyf_address')->where('uid='.$this->user['m_uid']." and status!=0")->select();
        $this->view->assign('arr', $arr);
        return $this->view->fetch(); 
    }
    public function moren(){
 
        if($this->request->isPost()){
            
            $ty = $_POST['ty'];
            $id = $_POST['id'];
            if ($ty==1) {
               $data['status']=1;
               Db::table('kyf_address')->where('uid='.$this->user['m_uid'])->update($data);
            }elseif ($ty==2) {
              $data['status']=1;
               Db::table('kyf_address')->where('uid='.$this->user['m_uid'])->update($data);
               $data['status']=2;
               Db::table('kyf_address')->where('address_id='.$id)->update($data);
            }else{
              $data['status']=0;
               Db::table('kyf_address')->where('address_id='.$id)->update($data);  
            }
            echo 1;exit;
        }
    }
    public function tianjiadizhi(){
        
        if($this->request->isPost()){
            
            $add['realname'] = $_POST['shouhuo'];
            $add['city_str'] = $_POST['dizhi'];
            $add['address'] = $_POST['xiangxi'];
            $add['mobile'] = $_POST['phone'];
            $add['status'] = $_POST['ty'];
            $add['create_time'] = time();
            $add['uid'] = $this->user['m_uid'];
            if ($add['status']==2) {
                $data['status']=1;
               Db::table('kyf_address')->where('uid='.$this->user['m_uid'])->update($data); 
            }
   
            $re = Db::table('kyf_address')->insert($add,false,true);
            echo $re;exit;
        }else{
          
          return $this->view->fetch();   
        } 
    }
     public function updizhi(){
        if($this->request->isPost()){
            
            $add['realname'] = $_POST['shouhuo'];
            $add['city_str'] = $_POST['dizhi'];
            $add['address'] = $_POST['xiangxi'];
            $add['mobile'] = $_POST['phone'];
            $add['status'] = $_POST['ty'];
            $add['create_time'] = time();
            $add['uid'] = $this->user['m_uid'];
            if ($add['status']==2) {
                $data['status']=1;
               Db::table('kyf_address')->where('uid='.$this->user['m_uid'])->update($data); 
            }
   
            $re = Db::table('kyf_address')->where('address_id='.$_POST['id'])->update($add); 
            echo $re;exit;
        }else{


        $id = $this->param['id'];
        $arr = Db::table('kyf_address')->where('address_id='.$id)->find();
        $this->view->assign('arr', $arr);
        return $this->view->fetch(); 
         }
    }
    public function shezhi(){

        $arr = Db::table('kyf_member_base')->where('m_uid='.$this->user['m_uid'])->find();
        $this->view->assign('arr', $arr);
        return $this->view->fetch(); 
    }
    public function guanyu(){
        return $this->view->fetch(); 
    }
     public function guanyu_sanhuo(){
        return $this->view->fetch(); 
    }
     public function guanyu_shucai(){
        return $this->view->fetch(); 
    }
     public function guanyu_haixian(){
        return $this->view->fetch(); 
    }
     public function guanyu_miaobao(){
        return $this->view->fetch(); 
    }
    public function xieyi(){

        return $this->view->fetch(); 
    }
    public function upload()
    {
        $file = $this->request->file('file');

        $path = ROOT_PATH . '/public/tmp/uploads/';
        $info = $file->move($path,true,false,false);

        
        $data =  $path . $info->getSaveName();
        
        $var = Session::get('jiatu');
        Session::set('jiatu',$var.$data.",");


    }
    public function uploads()
    {
        $file = $this->request->file('file');

        $path = ROOT_PATH . '/public/tmp/uploads/';
        $info = $file->move($path,true,false,false);

        
        $data['m_pic'] =  '/public/tmp/uploads/'.$info->getSaveName();
        Db::table('kyf_member_base')->where('m_uid='.$this->user['m_uid'])->update($data);
       


    }
    public function delorder(){
        $id = $_POST['id'];
        $sa['status'] = 4;
        $re = Db::table('kyf_order')->where("order_id=".$id)->update($sa);
        if($re){
            return 1;exit;
            //return ajax_return(array(),'移出成功');
        } else {
            return 2;exit;
            //return ajax_return(array(),'移出失败',1);
        }
    }
    

    public function delcang(){
        $id = $_POST['id'];
        $re = Db::table('kyf_shoucang')->where("id=".$id)->delete();
        if($re){
            return 1;exit;
            //return ajax_return(array(),'移出成功');
        } else {
            return 2;exit;
            //return ajax_return(array(),'移出失败',1);
        }
    }
    public function liulan()
    {
      $maps['uid'] = $this->user['m_uid'];
     
      $arr = Db::table('kyf_liulan')->where($maps)->select(); 
      foreach ($arr as $key => $value) {
          $re = Db::table('kyf_goods')->where("goods_id=".$value['gid'])->find();
          $arr[$key]['goods_name'] = $re['goods_name'];
          $picarr = explode(',',$re['goods_pics']);
          $arr[$key]['goods_pic'] = $picarr[0];
          $arr[$key]['buytype'] = $re['buytype'];
          $arr[$key]['price'] = fmoney($re['price'],false);
      }
      $this->view->assign('arr', $arr);
      return $this->view->fetch();
    }

    

}